//
//  NewFeatureViewController.swift
//  WBDemo
//
//  Created by Meng on 17/2/13.
//  Copyright © 2017年 demo. All rights reserved.
//

import UIKit
import SnapKit

class NewFeatureViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    private let maxCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //newFeatureCell
    // MARK: - UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newFeatureCell", for: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    // MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        // 注意: 传入的cell和indexPath都是上一页的, 而不是当前页
        let index = collectionView.indexPathsForVisibleItems.last!
        let currentCell = collectionView.cellForItem(at: index) as! NewFeatureCell
        
        if index.item == maxCount - 1 {
            currentCell.startAnimation()
        }
    }
}

// 自定义布局
class NewFeatureLayout: UICollectionViewFlowLayout {
    override func prepare() {
        itemSize = UIScreen.main.bounds.size
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.horizontal
        collectionView?.bounces = false
        collectionView?.isPagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
    }
}

// 自定义cell
class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0 {
        didSet {
            let name = "new_feature_\(index+1)"
            iconView.image = UIImage(named: name)
            startButton.isHidden = true
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    func setupUI() {
        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        contentView.addSubview(startButton)
        startButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(-120)
        }
    }
    
    // MARK: - 懒加载
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "new_feature_button"), for: .normal)
        button.isHidden = true
        button.addTarget(self, action: #selector(beginWeibo), for: .touchUpInside)
        return button
    }()
    
    // MARK: - 执行动画
    func startAnimation() {
        startButton.isHidden = false
        startButton.isUserInteractionEnabled = false
        startButton.transform = CGAffineTransform(scaleX: 0.0, y: 0.0)
        /*
         第一个参数: 动画时间
         第二个参数: 延迟时间
         第三个参数: 震幅 0.0~1.0, 值越小震动越列害
         第四个参数: 加速度, 值越大震动越列害
         第五个参数: 动画附加属性
         第六个参数: 执行动画的block
         第七个参数: 执行完毕后回调的block
         */
        UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions.curveEaseInOut, animations: {
            self.startButton.transform = CGAffineTransform.identity
        }) { (Bool) in
            self.startButton.isUserInteractionEnabled = true
        }
    }
    
    // MARK: - 进入微博
    @objc private func beginWeibo() {
        NotificationCenter.default.post(name: NSNotification.Name.init(SwitchRootViewController), object: true)
    }
}
